
// A3c_price_index_full_adj
//==============================================================================

// Description: This code computes the extent of inequality in the gains from 
// trade for full model

clear
set more off

cd "D:\data_replication"

use robustness\nested_CES\P_summary_full_adj_format.dta

// Exclude extreme price index value
//------------------------------------------------------------------------------ 

drop if p_true_k_sorted_20 < 1e-10
drop if p_true_k_sorted_80 < 1e-10
drop if p_counter_k_sorted_20 < 1e-10
drop if p_counter_k_sorted_80 < 1e-10

drop if p_true_k_sorted_20 > 1e+10
drop if p_true_k_sorted_80 > 1e+10
drop if p_counter_k_sorted_20 > 1e+10
drop if p_counter_k_sorted_80 > 1e+10

// Normalize Income draws
//------------------------------------------------------------------------------ 

replace y_20 = y_20 + 4.162364
replace y_20 = y_20 / 7.1021707
replace y_80 = y_80 + 4.162364
replace y_80 = y_80 / 7.1021707

gen c0 = 2.18
gen c1 = -0.63

gen c_sigma_20 = c0 + c1 * y_20
gen c_sigma_80 = c0 + c1 * y_80

gen p_true_k_20_sigma = p_true_k_sorted_20 ^ (1 - c_sigma_20)
gen p_true_k_80_sigma = p_true_k_sorted_80 ^ (1 - c_sigma_80)
gen p_counter_k_20_sigma = p_counter_k_sorted_20 ^ (1 - c_sigma_20)
gen p_counter_k_80_sigma = p_counter_k_sorted_80 ^ (1 - c_sigma_80)

drop if p_true_k_20_sigma > 10000
drop if p_true_k_80_sigma > 10000
drop if p_counter_k_20_sigma > 10000
drop if p_counter_k_80_sigma > 10000

sort year quarter declarant
by year quarter declarant: egen P20_true = sum(p_true_k_20_sigma)
by year quarter declarant: egen P80_true = sum(p_true_k_80_sigma)
by year quarter declarant: egen P20_counter = sum(p_counter_k_20_sigma)
by year quarter declarant: egen P80_counter = sum(p_counter_k_80_sigma)

gen dP_20 = (P20_counter/P20_true) ^(1 / (1 - c_sigma_20))
gen dP_80 = (P80_counter/P80_true) ^(1 / (1 - c_sigma_80))

gen diff_temp = p_counter_k_20_sigma/p_true_k_20_sigma - p_counter_k_80_sigma/p_true_k_80_sigma
gen diff_temp_orig = p_counter_k_sorted_20/p_true_k_sorted_20 - p_counter_k_sorted_80/p_true_k_sorted_80
gen dp_20 = p_counter_k_sorted_20/ p_true_k_sorted_20
gen dp_80 = p_counter_k_sorted_80/ p_true_k_sorted_80


gen diff = dP_20 - dP_80

sort year quarter declarant
by year quarter declarant: keep if _n == 1

keep year quarter declarant diff
export delimited using robustness\nested_CES\results\price_index\diff_full_adj_actual.csv, replace


